package com.tb.shoop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tb.v1.api.shoop.ISpPermissionService;
import com.tb.v1.api.vo.SpPermissionVo;
import com.tb.v1.entity.SpPermission;
import com.tb.v1.result.ResultJson;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author: tangBin
 * @Date: 2020/7/14 0014 11:09
 * @Description:
 */
@RestController
@RequestMapping("/v1")
public class SpPermissionController extends BaseController {

    @Reference
    private ISpPermissionService spPermissionService;

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 首页左侧菜单方法
     * @Date 2020/7/20 0020 10:45
     * @Param []
     **/
    @RequestMapping("menus")
    public ResultJson<Object> getMenus() {
        List<SpPermission> menus = spPermissionService.getMenus(0);
        List<Object> results = new ArrayList<>();
        if (!menus.isEmpty()) {
            List<SpPermission> children = null;
            Map map = null;
            for (SpPermission menu : menus) {
                if (!StringUtils.isEmpty(menu.getPsId())) {
                    children = spPermissionService.getMenus(menu.getPsId());
                    map = JSON.parseObject(JSON.toJSONString(menu), Map.class);
                    map.put("children", children);
                    results.add(map);
                }
            }

        }
        return toResultModel("200", "成功", results);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 角色列表方法
     * @Date 2020/7/20 0020 10:45
     * @Param [type]
     **/
    @RequestMapping("rights/{type}")
    public ResultJson<Object> getRights(@PathVariable("type") String type) {
        Map map = new HashMap();
        List<SpPermission> list = spPermissionService.getRightsList(map);
        Integer total = 0;
        if (!list.isEmpty()) {
            total = spPermissionService.getRightsCount(map);
        }
        Map resultMap = new HashMap();
        //resultMap.put("pageIndex",page);
        //resultMap.put("total",total);
        resultMap.put("rights", list);
        return toResultModel("200", "成功", resultMap);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 删除权限后， 返回角色下面所有权限 菜单列表
     * @Date 2020/7/20 0020 10:44
     * @Param []
     **/
    @RequestMapping("rightsList")
    public ResultJson<Object> getRightsList() {
        Map map = new HashMap();
        map.put("psPid", 0);
        //1级权限
        List resultList = new ArrayList();
        List<SpPermission> oneList = spPermissionService.getPermissionList(map);
        if (!oneList.isEmpty()) {
            Map oneMap = null;
            Map twoMap = null;
            List<SpPermission> rightsList = null;
            List<SpPermission> rightsList1 = null;
            List<Object> twoList = null;
            for (SpPermission sp : oneList) {
                twoList = new ArrayList<>();
                //获取2级权限
                map.put("psPid", sp.getPsId());
                rightsList = spPermissionService.getPermissionList(map);
                if (!rightsList.isEmpty()) {
                    for (SpPermission permission : rightsList) {
                        //三级权限
                        map.put("psPid", permission.getPsId());
                        rightsList1 = spPermissionService.getPermissionList(map);
                        twoMap = JSON.parseObject(JSON.toJSONString(permission));
                        twoMap.put("children", rightsList1);
                        twoList.add(twoMap);
                    }
                }
                oneMap = JSON.parseObject(JSON.toJSONString(sp));
                oneMap.put("children", twoList);
                resultList.add(oneMap);
            }
        }
        return toResultModel("200", "成功", resultList);
    }
}
